package dominioProblema;

import java.util.Random;

import br.ufsc.inf.leobr.cliente.Jogada;

public class Armazem implements Jogada{

	private static final long serialVersionUID = 1L;
	protected Pedra[] pedras;
	protected int quantPedras;


	public Armazem(){
		this.pedras = new Pedra[28];
		this.quantPedras = 28;			
		for (int idP=0; idP<28; idP++){
			int pa = 0;
			int pb = 0;
			while (pa==0){
				while (pb<7){
					int id = idP;
					this.pedras[id] = new Pedra(id, pa, pb);
					pb++;
					id++;
					idP = id;
				}
				pa++;
				pb=1;
			}
			while (pa==1){
				while (pb<7){
					int id = idP;
					this.pedras[id] = new Pedra(id, pa, pb);
					pb++;
					id++;
					idP = id;
				}
				pa++;
				pb=2;
			}
			while (pa==2){
				while (pb<7){
					int id = idP;
					this.pedras[id] = new Pedra(id, pa, pb);
					pb++;
					id++;
					idP = id;
				}
				pa++;
				pb=3;
			}
			while (pa==3){
				while (pb<7){
					int id = idP;
					this.pedras[id] = new Pedra(id, pa, pb);
					pb++;
					id++;
					idP = id;
				}
				pa++;
				pb=4;
			}
			while (pa==4){
				while (pb<7){
					int id = idP;
					this.pedras[id] = new Pedra(id, pa, pb);
					pb++;
					id++;
					idP = id;
				}
				pa++;
				pb=5;
			}
			while (pa==5){
				while (pb<7){
					int id = idP;
					if (pa!=pb){
						this.pedras[id] = new Pedra(id, pa, pb);
						pb++;
						id++;
						idP = id;
					}else{
						boolean double5 = true;
						this.pedras[id] = new Pedra(id, pa, pb, double5);
						pb++;
						id++;
						idP = id;
					}
				}
				pa++;
				pb=6;
			}
			this.pedras[idP] = new Pedra(idP, pa, pb);
		}
	}

	public Pedra[] sorteiaPedras(){
		Pedra[] p = new Pedra[5];
		int i = 0;
		while (i<5){
			Random r = new Random();			
			int sorteio = r.nextInt(28);			
			if (pedras[sorteio]!=null){
				p[i] = pedras[sorteio];
				pedras[sorteio] = null;
				i++;
			}	
		}
		this.quantPedras = this.quantPedras-5;
		return p;
	}

	public Pedra[] getPedras() {
		return pedras;
	}

	public int getQuantPedras() {
		return quantPedras;
	}

	public Pedra vendePedra() {
		Pedra p = new Pedra();
		Random r = new Random();
		boolean vender = false;
		while (!vender){
			int sorteio = r.nextInt(28);
			if (this.pedras[sorteio]!=null){
				p = this.pedras[sorteio];
				this.pedras[sorteio] = null;
				vender = true;
				this.quantPedras--;
			}
		}
		return p;
	}

	public void zerar() {
		this.quantPedras = 0;
		for (int i=0; i<28; i++){
			this.pedras[i] = null;
		}
	}

}